﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Framework.BasicFramework;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Core.IMessage;
using IPC.Communication.Framework.Core.Net;
using IPC.Communication.Framework.Reflection;

namespace IPC.Communication.Framework.Profinet.AllenBradley
{
    public class AllenBradleyNet : NetworkDeviceBase
    {
        private uint _sessionHandle;
        private byte _slot = 0;
        private byte[] _portSlot;
        private ushort _cipCommand = 111;

        public uint SessionHandle
        {
            get { return _sessionHandle; }
            protected set { _sessionHandle = value; }
        }

        public byte Slot
        {
            get { return _slot; }
            set { _slot = value; }
        }


        public byte[] PortSlot
        {
            get { return _portSlot; }
            set { _portSlot = value; }
        }

        public ushort CipCommand
        {
            get { return _cipCommand; }
            set { _cipCommand = value; }
        }


        public AllenBradleyNet()
        {
            base.WordLength = 2;
            base.ByteTransform = new RegularByteTransform();
        }

        public AllenBradleyNet(string ipAddress, int port = 44818)
            : this()
        {
            IpAddress = ipAddress;
            Port = port;
        }

        protected override INetMessage GetNewNetMessage()
        {
            return new AllenBradleyMessage();
        }

        protected override byte[] PackCommandWithHeader(byte[] command)
        {
            return AllenBradleyHelper.PackRequestHeader(CipCommand, SessionHandle, command);
        }

        protected override OperateResult InitializationOnConnect(Socket socket)
        {
            OperateResult<byte[]> operateResult = ReadFromCoreServer(socket, AllenBradleyHelper.RegisterSessionHandle(), hasResponseData: true, usePackAndUnpack: false);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult operateResult2 = AllenBradleyHelper.CheckResponse(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return operateResult2;
            }
            SessionHandle = base.ByteTransform.TransUInt32(operateResult.Content, 4);
            return OperateResult.CreateSuccessResult();
        }

        protected override OperateResult ExtraOnDisconnect(Socket socket)
        {
            OperateResult<byte[]> operateResult = ReadFromCoreServer(socket, AllenBradleyHelper.UnRegisterSessionHandle(SessionHandle), hasResponseData: true, usePackAndUnpack: false);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            return OperateResult.CreateSuccessResult();
        }

        //[AsyncStateMachine(typeof(<InitializationOnConnectAsync>d__22))]
        //[DebuggerStepThrough]
        //protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
        //{
        //    <InitializationOnConnectAsync>d__22 stateMachine = new <InitializationOnConnectAsync>d__22();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.socket = socket;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ExtraOnDisconnectAsync>d__23))]
        //[DebuggerStepThrough]
        //protected override Task<OperateResult> ExtraOnDisconnectAsync(Socket socket)
        //{
        //    <ExtraOnDisconnectAsync>d__23 stateMachine = new <ExtraOnDisconnectAsync>d__23();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.socket = socket;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        public virtual OperateResult<byte[]> BuildReadCommand(string[] address, int[] length)
        {
            if (address == null || length == null)
            {
                return new OperateResult<byte[]>("address or length is null");
            }
            if (address.Length != length.Length)
            {
                return new OperateResult<byte[]>("address and length is not same array");
            }
            try
            {
                byte b = Slot;
                List<byte[]> list = new List<byte[]>();
                for (int i = 0; i < address.Length; i++)
                {
                    b = (byte)HslHelper.ExtractParameter(ref address[i], "slot", Slot);
                    list.Add(AllenBradleyHelper.PackRequsetRead(address[i], length[i]));
                }
                byte[] value = AllenBradleyHelper.PackCommandSpecificData(new byte[4], PackCommandService(PortSlot ?? new byte[2]
			{
				1,
				b
			}, list.ToArray()));
                return OperateResult.CreateSuccessResult(value);
            }
            catch (Exception ex)
            {
                return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
            }
        }

        private OperateResult<byte[]> BuildReadCommand(string[] address)
        {
            if (address == null)
            {
                return new OperateResult<byte[]>("address or length is null");
            }
            int[] array = new int[address.Length];
            for (int i = 0; i < address.Length; i++)
            {
                array[i] = 1;
            }
            return BuildReadCommand(address, array);
        }

        private OperateResult<byte[]> BuildWriteCommand(string address, ushort typeCode, byte[] data, int length = 1)
        {
            try
            {
                byte b = (byte)HslHelper.ExtractParameter(ref address, "slot", Slot);
                byte[] array = AllenBradleyHelper.PackRequestWrite(address, typeCode, data, length);
                byte[] value = AllenBradleyHelper.PackCommandSpecificData(new byte[4], PackCommandService(PortSlot ?? new byte[2]
			{
				1,
				b
			}, array));
                return OperateResult.CreateSuccessResult(value);
            }
            catch (Exception ex)
            {
                return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
            }
        }

        private OperateResult<byte[]> BuildWriteCommand(string address, bool data)
        {
            try
            {
                byte b = (byte)HslHelper.ExtractParameter(ref address, "slot", Slot);
                byte[] array = AllenBradleyHelper.PackRequestWrite(address, data);
                byte[] value = AllenBradleyHelper.PackCommandSpecificData(new byte[4], PackCommandService(PortSlot ?? new byte[2]
			{
				1,
				b
			}, array));
                return OperateResult.CreateSuccessResult(value);
            }
            catch (Exception ex)
            {
                return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
            }
        }

        [HslMqttApi("ReadByteArray", "")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            if (length > 1)
            {
                return ReadSegment(address, 0, length);
            }
            return Read(new string[1]
		{
			address
		}, new int[1]
		{
			length
		});
        }

        [HslMqttApi("ReadAddress", "")]
        private OperateResult<byte[]> Read(string[] address)
        {
            if (address == null)
            {
                return new OperateResult<byte[]>("address can not be null");
            }
            int[] array = new int[address.Length];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = 1;
            }
            return Read(address, array);
        }

        public OperateResult<byte[]> Read(string[] address, int[] length)
        {
            OperateResult<byte[], ushort, bool> operateResult = ReadWithType(address, length);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult);
            }
            return OperateResult.CreateSuccessResult(operateResult.Content1);
        }

        private OperateResult<byte[], ushort, bool> ReadWithType(string[] address, int[] length)
        {
            OperateResult<byte[]> operateResult = BuildReadCommand(address, length);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[], ushort, bool>(operateResult);
            }
            OperateResult<byte[]> operateResult2 = ReadFromCoreServer(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[], ushort, bool>(operateResult2);
            }
            OperateResult operateResult3 = AllenBradleyHelper.CheckResponse(operateResult2.Content);
            if (!operateResult3.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[], ushort, bool>(operateResult3);
            }
            return AllenBradleyHelper.ExtractActualData(operateResult2.Content, isRead: true);
        }

        [HslMqttApi("ReadSegment", "")]
        private OperateResult<byte[]> ReadSegment(string address, int startIndex, int length)
        {
            try
            {
                List<byte> list = new List<byte>();
                OperateResult<byte[], ushort, bool> operateResult2;
                do
                {
                    OperateResult<byte[]> operateResult = ReadCipFromServer(AllenBradleyHelper.PackRequestReadSegment(address, startIndex, length));
                    if (!operateResult.IsSuccess)
                    {
                        return operateResult;
                    }
                    operateResult2 = AllenBradleyHelper.ExtractActualData(operateResult.Content, isRead: true);
                    if (!operateResult2.IsSuccess)
                    {
                        return OperateResult.CreateFailedResult<byte[]>(operateResult2);
                    }
                    startIndex += operateResult2.Content1.Length;
                    list.AddRange(operateResult2.Content1);
                }
                while (operateResult2.Content3);
                return OperateResult.CreateSuccessResult(list.ToArray());
            }
            catch (Exception ex)
            {
                return new OperateResult<byte[]>("Address Wrong:" + ex.Message);
            }
        }

        private OperateResult<byte[]> ReadByCips(params byte[][] cips)
        {
            OperateResult<byte[]> operateResult = ReadCipFromServer(cips);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult<byte[], ushort, bool> operateResult2 = AllenBradleyHelper.ExtractActualData(operateResult.Content, isRead: true);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult2);
            }
            return OperateResult.CreateSuccessResult(operateResult2.Content1);
        }

        private OperateResult<byte[]> ReadCipFromServer(params byte[][] cips)
        {
            byte[] send = AllenBradleyHelper.PackCommandSpecificData(new byte[4], PackCommandService(PortSlot ?? new byte[2]
		{
			1,
			Slot
		}, cips.ToArray()));
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult operateResult2 = AllenBradleyHelper.CheckResponse(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult2);
            }
            return OperateResult.CreateSuccessResult(operateResult.Content);
        }

        private OperateResult<byte[]> ReadEipFromServer(params byte[][] eip)
        {
            byte[] send = AllenBradleyHelper.PackCommandSpecificData(eip);
            OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult operateResult2 = AllenBradleyHelper.CheckResponse(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult2);
            }
            return OperateResult.CreateSuccessResult(operateResult.Content);
        }

        [HslMqttApi("ReadBool", "")]
        public override OperateResult<bool> ReadBool(string address)
        {
            if (address.StartsWith("i="))
            {
                address = address.Substring(2);
                int arrayIndex = 0;
                address = AllenBradleyHelper.AnalysisArrayIndex(address, out arrayIndex);
                OperateResult<bool[]> operateResult = ReadBoolArray(address + String.Format("[{0}]", arrayIndex / 32));
                if (!operateResult.IsSuccess)
                {
                    return OperateResult.CreateFailedResult<bool>(operateResult);
                }
                return OperateResult.CreateSuccessResult(operateResult.Content[arrayIndex % 32]);
            }
            OperateResult<byte[]> operateResult2 = Read(address, 1);
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<bool>(operateResult2);
            }
            return OperateResult.CreateSuccessResult(base.ByteTransform.TransBool(operateResult2.Content, 0));
        }

        [HslMqttApi("ReadBoolArrayAddress", "")]
        public OperateResult<bool[]> ReadBoolArray(string address)
        {
            OperateResult<byte[]> operateResult = Read(address, 1);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<bool[]>(operateResult);
            }
            return OperateResult.CreateSuccessResult(base.ByteTransform.TransBool(operateResult.Content, 0, operateResult.Content.Length));
        }

        [HslMqttApi("ReadByte", "")]
        public OperateResult<byte> ReadByte(string address)
        {
            return ByteTransformHelper.GetResultFromArray(Read(address, 1));
        }

        //[AsyncStateMachine(typeof(<ReadAsync>d__39))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
        //{
        //    <ReadAsync>d__39 stateMachine = new <ReadAsync>d__39();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__40))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadAsync(string[] address)
        //{
        //    <ReadAsync>d__40 stateMachine = new <ReadAsync>d__40();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__41))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadAsync(string[] address, int[] length)
        //{
        //    <ReadAsync>d__41 stateMachine = new <ReadAsync>d__41();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadWithTypeAsync>d__42))]
        //[DebuggerStepThrough]
        //private Task<OperateResult<byte[], ushort, bool>> ReadWithTypeAsync(string[] address, int[] length)
        //{
        //    <ReadWithTypeAsync>d__42 stateMachine = new <ReadWithTypeAsync>d__42();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[], ushort, bool>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadSegmentAsync>d__43))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadSegmentAsync(string address, int startIndex, int length)
        //{
        //    <ReadSegmentAsync>d__43 stateMachine = new <ReadSegmentAsync>d__43();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.startIndex = startIndex;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCipFromServerAsync>d__44))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadCipFromServerAsync(params byte[][] cips)
        //{
        //    <ReadCipFromServerAsync>d__44 stateMachine = new <ReadCipFromServerAsync>d__44();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.cips = cips;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadEipFromServerAsync>d__45))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadEipFromServerAsync(params byte[][] eip)
        //{
        //    <ReadEipFromServerAsync>d__45 stateMachine = new <ReadEipFromServerAsync>d__45();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.eip = eip;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadBoolAsync>d__46))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<bool>> ReadBoolAsync(string address)
        //{
        //    <ReadBoolAsync>d__46 stateMachine = new <ReadBoolAsync>d__46();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadBoolArrayAsync>d__47))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<bool[]>> ReadBoolArrayAsync(string address)
        //{
        //    <ReadBoolArrayAsync>d__47 stateMachine = new <ReadBoolArrayAsync>d__47();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadByteAsync>d__48))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte>> ReadByteAsync(string address)
        //{
        //    <ReadByteAsync>d__48 stateMachine = new <ReadByteAsync>d__48();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        private OperateResult<AbTagItem[]> TagEnumerator()
        {
            List<AbTagItem> list = new List<AbTagItem>();
            ushort startInstance = 0;
            while (true)
            {
                OperateResult<byte[]> operateResult = ReadCipFromServer(AllenBradleyHelper.GetEnumeratorCommand(startInstance));
                if (!operateResult.IsSuccess)
                {
                    return OperateResult.CreateFailedResult<AbTagItem[]>(operateResult);
                }
                OperateResult<byte[], ushort, bool> operateResult2 = AllenBradleyHelper.ExtractActualData(operateResult.Content, isRead: true);
                if (!operateResult2.IsSuccess)
                {
                    return OperateResult.CreateFailedResult<AbTagItem[]>(operateResult2);
                }
                if (operateResult.Content.Length < 43 || BitConverter.ToUInt16(operateResult.Content, 40) != 213)
                {
                    break;
                }
                int num = 44;
                while (num < operateResult.Content.Length)
                {
                    AbTagItem abTagItem = new AbTagItem();
                    abTagItem.InstanceID = BitConverter.ToUInt32(operateResult.Content, num);
                    startInstance = (ushort)(abTagItem.InstanceID + 1);
                    num += 4;
                    ushort num2 = BitConverter.ToUInt16(operateResult.Content, num);
                    num += 2;
                    abTagItem.Name = Encoding.ASCII.GetString(operateResult.Content, num, num2);
                    num += num2;
                    abTagItem.SymbolType = BitConverter.ToUInt16(operateResult.Content, num);
                    num += 2;
                    if ((abTagItem.SymbolType & 0x1000) != 4096 && !abTagItem.Name.StartsWith("__"))
                    {
                        list.Add(abTagItem);
                    }
                }
                if (!operateResult2.Content3)
                {
                    return OperateResult.CreateSuccessResult(list.ToArray());
                }
            }
            return new OperateResult<AbTagItem[]>(StringResources.Language.UnknownError);
        }

        //[AsyncStateMachine(typeof(<TagEnumeratorAsync>d__50))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<AbTagItem[]>> TagEnumeratorAsync()
        //{
        //    <TagEnumeratorAsync>d__50 stateMachine = new <TagEnumeratorAsync>d__50();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<AbTagItem[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [Obsolete("未测试通过")]
        public OperateResult<AbTagItem[]> StructTagEnumerator(AbTagItem structTag)
        {
            OperateResult<AbStructHandle> operateResult = ReadTagStructHandle(structTag);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<AbTagItem[]>(operateResult);
            }
            OperateResult<byte[]> operateResult2 = ReadCipFromServer(AllenBradleyHelper.GetStructItemNameType(structTag.SymbolType, operateResult.Content));
            if (!operateResult2.IsSuccess)
            {
                return OperateResult.CreateFailedResult<AbTagItem[]>(operateResult2);
            }
            if (operateResult2.Content.Length >= 43 && operateResult2.Content[40] == 204 && operateResult2.Content[41] == 0)
            {
                byte[] bytes = BitConverter.GetBytes(structTag.SymbolType);
                bytes[1] = (byte)(bytes[1] & 0xF);
                if (bytes[1] >= 15)
                {
                    return OperateResult.CreateSuccessResult(EnumSysStructItemType(operateResult2.Content, operateResult.Content).ToArray());
                }
                return OperateResult.CreateSuccessResult(EnumUserStructItemType(operateResult2.Content, operateResult.Content).ToArray());
            }
            return new OperateResult<AbTagItem[]>(StringResources.Language.UnknownError);
        }

        private OperateResult<AbStructHandle> ReadTagStructHandle(AbTagItem structTag)
        {
            OperateResult<byte[]> operateResult = ReadByCips(AllenBradleyHelper.GetStructHandleCommand(structTag.SymbolType));
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<AbStructHandle>(operateResult);
            }
            if (operateResult.Content.Length >= 43 && BitConverter.ToInt32(operateResult.Content, 40) == 131)
            {
                AbStructHandle abStructHandle = new AbStructHandle();
                abStructHandle.Count = BitConverter.ToUInt16(operateResult.Content, 44);
                abStructHandle.TemplateObjectDefinitionSize = BitConverter.ToUInt32(operateResult.Content, 50);
                abStructHandle.TemplateStructureSize = BitConverter.ToUInt32(operateResult.Content, 58);
                abStructHandle.MemberCount = BitConverter.ToUInt16(operateResult.Content, 66);
                abStructHandle.StructureHandle = BitConverter.ToUInt16(operateResult.Content, 72);
                return OperateResult.CreateSuccessResult(abStructHandle);
            }
            return new OperateResult<AbStructHandle>(StringResources.Language.UnknownError);
        }

        private List<AbTagItem> EnumSysStructItemType(byte[] Struct_Item_Type_buff, AbStructHandle structHandle)
        {
            List<AbTagItem> list = new List<AbTagItem>();
            if (Struct_Item_Type_buff.Length > 41 && Struct_Item_Type_buff[40] == 204 && Struct_Item_Type_buff[41] == 0 && Struct_Item_Type_buff[42] == 0)
            {
                int num = Struct_Item_Type_buff.Length - 40;
                byte[] array = new byte[num - 4];
                Array.Copy(Struct_Item_Type_buff, 44, array, 0, num - 4);
                byte[] array2 = new byte[structHandle.MemberCount * 8];
                Array.Copy(array, 0, array2, 0, structHandle.MemberCount * 8);
                byte[] array3 = new byte[array.Length - array2.Length + 1];
                Array.Copy(array, array2.Length - 1, array3, 0, array.Length - array2.Length + 1);
                ushort memberCount = structHandle.MemberCount;
                for (int i = 0; i < memberCount; i++)
                {
                    AbTagItem abTagItem = new AbTagItem();
                    int num2;
                    abTagItem.SymbolType = BitConverter.ToUInt16(array2, num2 = 8 * i + 2);
                    list.Add(abTagItem);
                }
                List<int> list2 = new List<int>();
                for (int j = 0; j < array3.Length; j++)
                {
                    if (array3[j] == 0)
                    {
                        list2.Add(j);
                    }
                }
                list2.Add(array3.Length);
                for (int k = 0; k < list2.Count; k++)
                {
                    if (k != 0)
                    {
                        int num3 = 0;
                        num3 = ((k + 1 < list2.Count) ? (list2[k + 1] - list2[k] - 1) : 0);
                        if (num3 > 0)
                        {
                            list[k - 1].Name = Encoding.ASCII.GetString(array3, list2[k] + 1, num3);
                        }
                    }
                }
            }
            return list;
        }

        private List<AbTagItem> EnumUserStructItemType(byte[] Struct_Item_Type_buff, AbStructHandle structHandle)
        {
            List<AbTagItem> list = new List<AbTagItem>();
            bool flag = false;
            int num = 0;
            if ((Struct_Item_Type_buff.Length > 41) & (Struct_Item_Type_buff[40] == 204) & (Struct_Item_Type_buff[41] == 0) & (Struct_Item_Type_buff[42] == 0))
            {
                int num2 = Struct_Item_Type_buff.Length - 40;
                byte[] array = new byte[num2 - 4];
                Array.ConstrainedCopy(Struct_Item_Type_buff, 44, array, 0, num2 - 4);
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] == 0 && !flag)
                    {
                        num = i;
                    }
                    if (array[i] != 59 || array[i + 1] != 110)
                    {
                        continue;
                    }
                    flag = true;
                    int num3 = i - num - 1;
                    byte[] destinationArray = new byte[num3];
                    Array.Copy(array, num + 1, destinationArray, 0, num3);
                    byte[] array2 = new byte[i + 1];
                    Array.Copy(array, 0, array2, 0, i + 1);
                    byte[] array3 = new byte[array.Length - i - 1];
                    Array.Copy(array, i + 1, array3, 0, array.Length - i - 1);
                    if ((num + 1) % 8 != 0)
                    {
                        break;
                    }
                    int num4 = (num + 1) / 8 - 1;
                    for (int j = 0; j <= num4; j++)
                    {
                        AbTagItem abTagItem = new AbTagItem();
                        int num5;
                        abTagItem.SymbolType = BitConverter.ToUInt16(array2, num5 = 8 * j + 2);
                        list.Add(abTagItem);
                    }
                    List<int> list2 = new List<int>();
                    for (int k = 0; k < array3.Length; k++)
                    {
                        if (array3[k] == 0)
                        {
                            list2.Add(k);
                        }
                    }
                    list2.Add(array3.Length);
                    for (int l = 0; l < list2.Count; l++)
                    {
                        int num6 = 0;
                        num6 = ((l + 1 < list2.Count) ? (list2[l + 1] - list2[l] - 1) : 0);
                        if (num6 > 0)
                        {
                            list[l].Name = Encoding.ASCII.GetString(array3, list2[l] + 1, num6);
                        }
                    }
                    break;
                }
            }
            return list;
        }

        [HslMqttApi("ReadInt16Array", "")]
        public override OperateResult<short[]> ReadInt16(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransInt16(m, 0, length));
        }

        [HslMqttApi("ReadUInt16Array", "")]
        public override OperateResult<ushort[]> ReadUInt16(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransUInt16(m, 0, length));
        }

        [HslMqttApi("ReadInt32Array", "")]
        public override OperateResult<int[]> ReadInt32(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransInt32(m, 0, length));
        }

        [HslMqttApi("ReadUInt32Array", "")]
        public override OperateResult<uint[]> ReadUInt32(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransUInt32(m, 0, length));
        }

        [HslMqttApi("ReadFloatArray", "")]
        public override OperateResult<float[]> ReadFloat(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransSingle(m, 0, length));
        }

        [HslMqttApi("ReadInt64Array", "")]
        public override OperateResult<long[]> ReadInt64(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransInt64(m, 0, length));
        }

        [HslMqttApi("ReadUInt64Array", "")]
        public override OperateResult<ulong[]> ReadUInt64(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransUInt64(m, 0, length));
        }

        [HslMqttApi("ReadDoubleArray", "")]
        public override OperateResult<double[]> ReadDouble(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => base.ByteTransform.TransDouble(m, 0, length));
        }

        public OperateResult<string> ReadString(string address)
        {
            return ReadString(address, 1, Encoding.ASCII);
        }

        public override OperateResult<string> ReadString(string address, ushort length, Encoding encoding)
        {
            OperateResult<byte[]> operateResult = Read(address, length);
            if (!operateResult.IsSuccess)
            {
                return OperateResult.CreateFailedResult<string>(operateResult);
            }
            try
            {
                if (operateResult.Content.Length >= 6)
                {
                    int count = base.ByteTransform.TransInt32(operateResult.Content, 2);
                    return OperateResult.CreateSuccessResult(encoding.GetString(operateResult.Content, 6, count));
                }
                return OperateResult.CreateSuccessResult(encoding.GetString(operateResult.Content));
            }
            catch (Exception ex)
            {
                return new OperateResult<string>(ex.Message + " Source: " + operateResult.Content.ToHexString(' '));
            }
        }

        //[AsyncStateMachine(typeof(<ReadInt16Async>d__65))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<short[]>> ReadInt16Async(string address, ushort length)
        //{
        //    <ReadInt16Async>d__65 stateMachine = new <ReadInt16Async>d__65();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<short[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt16Async>d__66))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<ushort[]>> ReadUInt16Async(string address, ushort length)
        //{
        //    <ReadUInt16Async>d__66 stateMachine = new <ReadUInt16Async>d__66();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ushort[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt32Async>d__67))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<int[]>> ReadInt32Async(string address, ushort length)
        //{
        //    <ReadInt32Async>d__67 stateMachine = new <ReadInt32Async>d__67();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt32Async>d__68))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<uint[]>> ReadUInt32Async(string address, ushort length)
        //{
        //    <ReadUInt32Async>d__68 stateMachine = new <ReadUInt32Async>d__68();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<uint[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFloatAsync>d__69))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<float[]>> ReadFloatAsync(string address, ushort length)
        //{
        //    <ReadFloatAsync>d__69 stateMachine = new <ReadFloatAsync>d__69();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<float[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt64Async>d__70))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<long[]>> ReadInt64Async(string address, ushort length)
        //{
        //    <ReadInt64Async>d__70 stateMachine = new <ReadInt64Async>d__70();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<long[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt64Async>d__71))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<ulong[]>> ReadUInt64Async(string address, ushort length)
        //{
        //    <ReadUInt64Async>d__71 stateMachine = new <ReadUInt64Async>d__71();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ulong[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadDoubleAsync>d__72))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<double[]>> ReadDoubleAsync(string address, ushort length)
        //{
        //    <ReadDoubleAsync>d__72 stateMachine = new <ReadDoubleAsync>d__72();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<double[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStringAsync>d__73))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<string>> ReadStringAsync(string address)
        //{
        //    <ReadStringAsync>d__73 stateMachine = new <ReadStringAsync>d__73();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStringAsync>d__74))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult<string>> ReadStringAsync(string address, ushort length, Encoding encoding)
        //{
        //    <ReadStringAsync>d__74 stateMachine = new <ReadStringAsync>d__74();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.encoding = encoding;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("WriteByteArray", "")]
        public override OperateResult Write(string address, byte[] value)
        {
            return new OperateResult(StringResources.Language.NotSupportedFunction + " Please refer to use WriteTag instead ");
        }

        public virtual OperateResult WriteTag(string address, ushort typeCode, byte[] value, int length = 1)
        {
            OperateResult<byte[]> operateResult = BuildWriteCommand(address, typeCode, value, length);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            OperateResult<byte[]> operateResult2 = ReadFromCoreServer(operateResult.Content);
            if (!operateResult2.IsSuccess)
            {
                return operateResult2;
            }
            OperateResult operateResult3 = AllenBradleyHelper.CheckResponse(operateResult2.Content);
            if (!operateResult3.IsSuccess)
            {
                return OperateResult.CreateFailedResult<byte[]>(operateResult3);
            }
            return AllenBradleyHelper.ExtractActualData(operateResult2.Content, isRead: false);
        }

        //[AsyncStateMachine(typeof(<WriteAsync>d__77))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, byte[] value)
        //{
        //    <WriteAsync>d__77 stateMachine = new <WriteAsync>d__77();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteTagAsync>d__78))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteTagAsync(string address, ushort typeCode, byte[] value, int length = 1)
        //{
        //    <WriteTagAsync>d__78 stateMachine = new <WriteTagAsync>d__78();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.typeCode = typeCode;
        //    stateMachine.value = value;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        [HslMqttApi("WriteInt16Array", "")]
        public override OperateResult Write(string address, short[] values)
        {
            return WriteTag(address, 195, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteUInt16Array", "")]
        public override OperateResult Write(string address, ushort[] values)
        {
            return WriteTag(address, 199, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteInt32Array", "")]
        public override OperateResult Write(string address, int[] values)
        {
            return WriteTag(address, 196, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteUInt32Array", "")]
        public override OperateResult Write(string address, uint[] values)
        {
            return WriteTag(address, 200, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteFloatArray", "")]
        public override OperateResult Write(string address, float[] values)
        {
            return WriteTag(address, 202, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteInt64Array", "")]
        public override OperateResult Write(string address, long[] values)
        {
            return WriteTag(address, 197, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteUInt64Array", "")]
        public override OperateResult Write(string address, ulong[] values)
        {
            return WriteTag(address, 201, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteDoubleArray", "")]
        public override OperateResult Write(string address, double[] values)
        {
            return WriteTag(address, 203, base.ByteTransform.TransByte(values), values.Length);
        }

        [HslMqttApi("WriteString", "")]
        public override OperateResult Write(string address, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                value = string.Empty;
            }
            byte[] bytes = Encoding.ASCII.GetBytes(value);
            OperateResult operateResult = Write(address + ".LEN", bytes.Length);
            if (!operateResult.IsSuccess)
            {
                return operateResult;
            }
            byte[] value2 = SoftBasic.ArrayExpandToLengthEven(bytes);
            return WriteTag(address + ".DATA[0]", 194, value2, bytes.Length);
        }

        [HslMqttApi("WriteBool", "")]
        public override OperateResult Write(string address, bool value)
        {
            if (Regex.IsMatch(address, "\\[[0-9]+\\]$"))
            {
                OperateResult<byte[]> operateResult = BuildWriteCommand(address, value);
                if (!operateResult.IsSuccess)
                {
                    return operateResult;
                }
                OperateResult<byte[]> operateResult2 = ReadFromCoreServer(operateResult.Content);
                if (!operateResult2.IsSuccess)
                {
                    return operateResult2;
                }
                OperateResult operateResult3 = AllenBradleyHelper.CheckResponse(operateResult2.Content);
                if (!operateResult3.IsSuccess)
                {
                    return OperateResult.CreateFailedResult<byte[]>(operateResult3);
                }
                return AllenBradleyHelper.ExtractActualData(operateResult2.Content, isRead: false);
            }
            return WriteTag(address, 193, (!value) ? new byte[2] : new byte[2]
		{
			255,
			255
		});
        }

        [HslMqttApi("WriteByte", "")]
        public virtual OperateResult Write(string address, byte value)
        {
            return WriteTag(address, 194, new byte[2]
		{
			value,
			0
		});
        }

        //[AsyncStateMachine(typeof(<WriteAsync>d__90))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, short[] values)
        //{
        //    <WriteAsync>d__90 stateMachine = new <WriteAsync>d__90();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__91))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, ushort[] values)
        //{
        //    <WriteAsync>d__91 stateMachine = new <WriteAsync>d__91();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__92))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, int[] values)
        //{
        //    <WriteAsync>d__92 stateMachine = new <WriteAsync>d__92();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__93))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, uint[] values)
        //{
        //    <WriteAsync>d__93 stateMachine = new <WriteAsync>d__93();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__94))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, float[] values)
        //{
        //    <WriteAsync>d__94 stateMachine = new <WriteAsync>d__94();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__95))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, long[] values)
        //{
        //    <WriteAsync>d__95 stateMachine = new <WriteAsync>d__95();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__96))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, ulong[] values)
        //{
        //    <WriteAsync>d__96 stateMachine = new <WriteAsync>d__96();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__97))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, double[] values)
        //{
        //    <WriteAsync>d__97 stateMachine = new <WriteAsync>d__97();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__98))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, string value)
        //{
        //    <WriteAsync>d__98 stateMachine = new <WriteAsync>d__98();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__99))]
        //[DebuggerStepThrough]
        //public override Task<OperateResult> WriteAsync(string address, bool value)
        //{
        //    <WriteAsync>d__99 stateMachine = new <WriteAsync>d__99();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__100))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, byte value)
        //{
        //    <WriteAsync>d__100 stateMachine = new <WriteAsync>d__100();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        protected virtual byte[] PackCommandService(byte[] portSlot, params byte[][] cips)
        {
            return AllenBradleyHelper.PackCommandService(portSlot, cips);
        }

        public override string ToString()
        {
            return String.Format("AllenBradleyNet[{0}:{1}]", IpAddress, Port);
        }
    }
}
